<?php

namespace App\Http\Controllers\Admin\Crm;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Crm\ArticleCategory;

class CategoryController extends Controller
{
    public function __construct()
    {
        view()->share([
            'categories' => ArticleCategory::all_categories(),
            'cates' => ArticleCategory::all_cates(),  //用于select表单

            '_crm' => 'am-in',
            '_cat' => 'am-active'
        ]);

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.crm.category.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $category)
    {
        return view('admin.crm.category.create', compact('category'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        ArticleCategory::create($request->all());
        return redirect(route('crm.category.index'))->with('success', '新增分类成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $category)
    {
        return view('admin.crm.category.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $category = ArticleCategory::find($id);
        $category->update($request->all());
        return redirect(route('crm.category.index'))->with('success', '编辑分类成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $category = ArticleCategory::with('children')->find($id);
        if ($category->children->isEmpty()) {
            ArticleCategory::destroy($id);
            $info = array('status' => 1, 'msg' => '删除分类成功');
        } else {
            $info = array('status' => 0, 'msg' => '此分类下有二级分类，不能删除');
        }
        return $info;
    }


    /**
     * 改变属性
     */
    public function change_attr(Request $request)
    {
        $category = ArticleCategory::find($request->id);
        $attr = $request->attr;
        $category->$attr = !$category->$attr;
        $category->save();
    }

    /**
     * 排序
     */
    public function sort_order(Request $request)
    {
        $category = ArticleCategory::find($request->id);
        $category->sort_order = $request->sort_order;
        $category->save();
    }


}
